﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using BrnMall.Core;
using BrnMall.Core.Domain.WeiXin;
using System.Data.SqlClient;
using System.Data.Common;

namespace BrnMall.RDBSStrategy.SqlServer
{
	public partial class RDBSStrategy : IRDBSStrategy
	{
		#region 微信接入设置

		public void SaveWeiXinJoinConfig(JoinConfigInfo info)
		{
			if (info.Id > 0)
			{
				UpdateWeiXinJoinConfig(info);
			}
			else
			{
				InsertWeiXinJoinConfig(info);
			}
		}

		public IDataReader GetWeiXinJoinConfig()
		{
			var sql = "select * from {0}weixinjoinconfig";
			return RDBSHelper.ExecuteReader(CommandType.Text, string.Format(sql, RDBSHelper.RDBSTablePre));
		}

		private void InsertWeiXinJoinConfig(JoinConfigInfo info)
		{
			var sql = "insert into {0}weixinjoinconfig(Url,AppID,AppSecret,Token,EncodingAESKey,EncodingAESMode)";
			sql += "values(@Url,@AppID,@AppSecret,@Token,@EncodingAESKey,@EncodingAESMode);";

			RDBSHelper.ExecuteNonQuery(CommandType.Text, string.Format(sql, RDBSHelper.RDBSTablePre), new DbParameter[] {
				new SqlParameter("@Url",info.Url),
				new SqlParameter("@AppID",info.AppID),
				new SqlParameter("@AppSecret",info.AppSecret),
				new SqlParameter("@Token",info.Token),
				new SqlParameter("@EncodingAESKey",info.EncodingAESKey),
				new SqlParameter("@EncodingAESMode",info.EncodingAESMode)
			});
		}
		private void UpdateWeiXinJoinConfig(JoinConfigInfo info)
		{
			var sql = "update {0}weixinjoinconfig set";
			sql += " Url=@Url,AppID=@AppID,AppSecret=@AppSecret,Token=@Token,EncodingAESKey=@EncodingAESKey,EncodingAESMode=@EncodingAESMode";
			sql += " where Id=@Id";

			RDBSHelper.ExecuteNonQuery(CommandType.Text, string.Format(sql, RDBSHelper.RDBSTablePre), new DbParameter[] {
				new SqlParameter("@Id",info.Id),
				new SqlParameter("@Url",info.Url),
				new SqlParameter("@AppID",info.AppID),
				new SqlParameter("@AppSecret",info.AppSecret),
				new SqlParameter("@Token",info.Token),
				new SqlParameter("@EncodingAESKey",info.EncodingAESKey),
				new SqlParameter("@EncodingAESMode",info.EncodingAESMode)
			});
		}

		#endregion

		#region 微信公众号菜单设置

		public void SaveWeiXinMenus(WeiXinMenu[] menus)
		{
			var paramList = new List<DbParameter>();
			var sql = new StringBuilder(string.Format("delete {0}WeiXinMenus;", RDBSHelper.RDBSTablePre));
			var sqlTemp = "insert into {0}WeiXinMenus(Name,Url,ParentId) values(@Name{1},@Url{1},@ParentId{1});";

			for (var i = 0; i < menus.Length; i++)
			{
				sql.Append(string.Format(sqlTemp, RDBSHelper.RDBSTablePre, i));
				paramList.AddRange(new DbParameter[] {
					new SqlParameter("@Name"+i,menus[i].Name),
					new SqlParameter("@Url"+i,menus[i].Url),
					new SqlParameter("@ParentId"+i,menus[i].ParentId),
				});
			}

			var conn = new SqlConnection(RDBSHelper.ConnectionString);
			var tran = conn.BeginTransaction();
			RDBSHelper.ExecuteNonQuery(tran, CommandType.Text, sql.ToString(), paramList.ToArray());
		}
		public DataTable GetWeiXinMenus()
		{
			var sql = "select * from {0}WeiXinMenus";
			return RDBSHelper.ExecuteDataset(CommandType.Text, string.Format(sql, RDBSHelper.RDBSTablePre)).Tables[0];
		}

		#endregion
	}
}
